import java.util.HashSet;
import java.util.Random;
import java.util.Set;

/**
 * 加减法口算练习系统-v0.3
 * 生成50道不重复的100以内加法/减法算式，并显示答案
 */
public class CalculationSystemV03 {
    
    public static void main(String[] args) {
        System.out.println("加减法口算练习系统-v0.3");
        System.out.println("==========================================");
        
        generateUniqueExercises(50);
        
        System.out.println("==========================================");
        System.out.println("练习结束！");
    }
    
    /**
     * 生成指定数量的不重复加减法算式习题并显示答案
     * @param count 习题数量
     */
    public static void generateUniqueExercises(int count) {
        Random random = new Random();
        Set<String> generatedExercises = new HashSet<>(); // 用于存储已生成的算式，确保不重复
        
        // 每行显示8道题
        int exercisesPerLine = 8;
        
        // 确保加法和减法都有足够的题目，这里设置大约各占一半
        int additionCount = 0;
        int subtractionCount = 0;
        final int minEachType = count / 4; // 每种类型至少要有一定数量
        
        int generated = 0;
        while (generated < count) {
            String exercise;
            int firstNumber, secondNumber, answer;
            
            // 决定当前要生成的运算类型
            boolean isAddition;
            if (additionCount < minEachType) {
                isAddition = true; // 确保加法至少有一定数量
            } else if (subtractionCount < minEachType) {
                isAddition = false; // 确保减法至少有一定数量
            } else {
                isAddition = random.nextBoolean(); // 之后随机决定
            }
            
            if (isAddition) {
                // 加法：确保结果不超过100
                firstNumber = random.nextInt(100);
                secondNumber = random.nextInt(101 - firstNumber); // 确保firstNumber + secondNumber <= 100
                answer = firstNumber + secondNumber;
                exercise = "(" + (generated + 1) + ")" + firstNumber + " + " + secondNumber + " = " + answer;
                
                // 检查是否重复（考虑加法交换律，如1+2和2+1视为同一算式）
                String canonicalForm;
                if (firstNumber <= secondNumber) {
                    canonicalForm = "ADD:" + firstNumber + ":" + secondNumber;
                } else {
                    canonicalForm = "ADD:" + secondNumber + ":" + firstNumber;
                }
                
                if (generatedExercises.add(canonicalForm)) {
                    // 成功添加，说明不重复
                    System.out.print(exercise);
                    additionCount++;
                    generated++;
                    
                    // 格式化输出
                    if (generated % exercisesPerLine != 0 && generated < count) {
                        System.out.print("  ");
                    } else {
                        System.out.println();
                    }
                }
            } else {
                // 减法：确保结果不为负数
                firstNumber = random.nextInt(100) + 1; // 被减数至少为1
                secondNumber = random.nextInt(firstNumber + 1); // 减数不大于被减数
                answer = firstNumber - secondNumber;
                exercise = "(" + (generated + 1) + ")" + firstNumber + " - " + secondNumber + " = " + answer;
                
                // 检查是否重复（减法不考虑交换律）
                String canonicalForm = "SUB:" + firstNumber + ":" + secondNumber;
                
                if (generatedExercises.add(canonicalForm)) {
                    // 成功添加，说明不重复
                    System.out.print(exercise);
                    subtractionCount++;
                    generated++;
                    
                    // 格式化输出
                    if (generated % exercisesPerLine != 0 && generated < count) {
                        System.out.print("  ");
                    } else {
                        System.out.println();
                    }
                }
            }
        }
        
        // 输出统计信息
        System.out.println("\n统计信息：");
        System.out.println("加法题目数量：" + additionCount);
        System.out.println("减法题目数量：" + subtractionCount);
    }
}